package com.gusto.engine.clusterant.algorithms.antsclustering;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gusto.engine.clusterant.model.Point;

public class PlanObject implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private int rows, cols;
	private List<Point> points;
	private Map<Integer, Map<Integer, Object>> rowsMap;
	
	
	public PlanObject() {
		super();
		rowsMap = new HashMap<Integer, Map<Integer, Object>>();
		for (int i = 0; i < rows; i++) {
			rowsMap.put(i, new HashMap<Integer, Object>());
		}
		this.points = new ArrayList<Point>();
	}
	
	public PlanObject(int rows, int cols, List<Point> points) {
		this();
		
		rowsMap = new HashMap<Integer, Map<Integer, Object>>();
		for (int i = 0; i < rows; i++) {
			rowsMap.put(i, new HashMap<Integer, Object>());
		}
		this.points = new ArrayList<Point>();
		
		this.rows = rows;
		this.cols = cols;
		for (Point p : points) {
			putObject(p.getRow(), p.getCol(), p.getObject());
		}
	}
	
	public String toString() {
		String result = "\n";
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				String cell = ""+(!isEmpty(i, j) ? (getObject(i, j)!=null ? getObject(i, j) : "X") : "-");
				if (cell.equals("-")) { cell = " - "; }
				if (cell.equals("X")) { cell = " X "; }
				if (cell.length()==1) { cell = " " + cell + " "; }
				if (cell.length()==2) { cell = " " + cell; }
				//if (cell.length()==3) { cell = cell; }
				result += cell;
			}
			result += "\n";
		}
		return result;
	}
	
	public boolean isEmpty(int row, int col) {
		return !rowsMap.get(row).containsKey(col);
	}
	
	public Object getObject(int row, int col) {
		return rowsMap.get(row).get(col);
	}
	
	public Point putObject(int row, int col, Object object) {
		rowsMap.get(row).put(col, object);
		Point point = new Point(row, col, object);
		points.add(point);
		return point;
	}
	
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public int getCols() {
		return cols;
	}
	public void setCols(int cols) {
		this.cols = cols;
	}
	
	public List<Point> getPoints() {
		return points;
	}
	public void setPoints(List<Point> points) {
		this.points = points;
	}
	
}
